const os = require('os');
const path = require('path');
const fs = require('fs');

// 获取storage.json路径
function getStoragePath() {
    const platform = os.platform();
    let basePath;

    switch (platform) {
        case 'win32':
            basePath = path.join(os.homedir(), 'AppData', 'Roaming', 'Cursor', 'User', 'globalStorage');
            break;
        case 'darwin':
            basePath = path.join(os.homedir(), 'Library', 'Application Support', 'Cursor', 'User', 'globalStorage');
            break;
        case 'linux':
            basePath = path.join(os.homedir(), '.config', 'Cursor', 'User', 'globalStorage');
            break;
        default:
            throw new Error('不支持的操作系统');
    }

    return path.join(basePath, 'storage.json');
}

// 生成随机机器码
function generateRandomMachineId() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        const r = (Math.random() * 16) | 0;
        const v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}

// 修改机器码
function modifyMacMachineId() {
    try {
        const storagePath = getStoragePath();

        if (!fs.existsSync(storagePath)) {
            throw new Error(`文件不存在: ${storagePath}`);
        }

        let data = JSON.parse(fs.readFileSync(storagePath, 'utf8'));
        const oldMachineId = data['telemetry.macMachineId'] || '未找到原机器码';
        const newMachineId = generateRandomMachineId();
        data['telemetry.macMachineId'] = newMachineId;

        fs.writeFileSync(storagePath, JSON.stringify(data, null, 2), 'utf8');

        return {
            success: true,
            message: '已成功修改机器码',
            oldId: oldMachineId,
            newId: newMachineId,
            path: storagePath
        };
    } catch (error) {
        throw new Error(`修改失败: ${error.message}`);
    }
}

window.exports = {
    "cursor-fake": {
        mode: "list",
        args: {
            // 进入插件时显示主菜单
            enter: (action, callbackSetList) => {
                callbackSetList([
                    {
                        title: '修改Cursor机器码',
                        description: '点击执行修改操作',
                        icon: 'logo.png',
                        url: 'modify'
                    },
                    {
                        title: '查看当前配置路径',
                        description: '显示storage.json的位置',
                        icon: 'logo.png',
                        url: 'path'
                    }
                ])
            },
            // 用户选择列表项时的操作
            select: (action, itemData, callbackSetList) => {
                if (itemData.url === 'modify') {
                    try {
                        const result = modifyMacMachineId();
                        callbackSetList([
                            {
                                title: '修改成功！',
                                description: `新机器码：${result.newId}`,
                                icon: 'logo.png'
                            },
                            {
                                title: '原机器码',
                                description: result.oldId,
                                icon: 'logo.png'
                            },
                            {
                                title: '复制新机器码',
                                description: result.newId,
                                icon: 'logo.png',
                                url: 'copy-new-id'
                            },
                            {
                                title: '返回主菜单',
                                description: '返回到主菜单',
                                icon: 'logo.png',
                                url: 'back'
                            }
                        ]);
                    } catch (error) {
                        callbackSetList([
                            {
                                title: '修改失败',
                                description: error.message,
                                icon: 'logo.png'
                            },
                            {
                                title: '返回主菜单',
                                description: '返回到主菜单',
                                icon: 'logo.png',
                                url: 'back'
                            }
                        ]);
                    }
                } else if (itemData.url === 'path') {
                    try {
                        const storagePath = getStoragePath();
                        callbackSetList([
                            {
                                title: '配置文件路径',
                                description: storagePath,
                                icon: 'logo.png'
                            },
                            {
                                title: '复制路径',
                                description: '复制配置文件路径到剪贴板',
                                storagePath: storagePath,
                                icon: 'logo.png',
                                url: 'copy-path'
                            },
                            {
                                title: '返回主菜单',
                                description: '返回到主菜单',
                                icon: 'logo.png',
                                url: 'back'
                            }
                        ]);
                    } catch (error) {
                        callbackSetList([
                            {
                                title: '获取路径失败',
                                description: error.message,
                                icon: 'logo.png'
                            },
                            {
                                title: '返回主菜单',
                                description: '返回到主菜单',
                                icon: 'logo.png',
                                url: 'back'
                            }
                        ]);
                    }
                } else if (itemData.url === 'copy-new-id') {
                    utools.copyText(itemData.description);
                    utools.showNotification('已复制新机器码到剪贴板');
                } else if (itemData.url === 'copy-path') {
                    utools.copyText(itemData.storagePath);
                    utools.showNotification('已复制路径到剪贴板');
                } else if (itemData.url === 'back') {
                    // 返回主菜单
                    window.exports['cursor-fake'].args.enter(action, callbackSetList);
                }
            }
        }
    }
} 